<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Restaurant extends MY_Controller {
  function __construct(){
    parent::__construct();

    $this->load->helper('form');
    $this->load->helper('url');
  }

  function index(){
      $this->_assign("isIndex", TRUE);
      $this->load->model('restaurant_model','restaurantModel');
      $where["order"] = "pos_cr_date desc";
      $where["where"]["status"] = "1";
      $where["limit"] = "3";
      $restaurantResult = $this->restaurantModel->getTag($where);
      $this->_assign("resList",$restaurantResult);
      //var_dump($restaurantResult);exit();
    $this->_fetch("index");
  }

  function admin_index(){
    $this->_assign("adminIndex", TRUE);
    $this->load->model('restaurant_model','restaurantModel');
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $where["order"] = "pos_cr_date desc";
    $where["where"]["status"] = "1";
    $where["where"]["parent_id"] = "0";
    $where["where"]["img_firstimage"] = "1";
    $restaurantResult = $this->restaurantModel->getin($where);
    $this->_assign("resList",$restaurantResult);
    //var_dump($restaurantResult);exit();

    $this->_fetch("admin_index");
  }
  function search(){
    $this->_assign("adminIndex", TRUE);
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $this->load->model('restaurant_model','restaurantModel');
    $where["order"] = "pos_cr_date desc";
    $where["where"]["parent_id"] = "0";
    $where["like"]["name"] = $_POST['name'];
    $where["where"]["img_firstimage"] = "1";
    $restaurantResult = $this->restaurantModel->getin($where);
    $this->_assign("resList",$restaurantResult);
    //var_dump($restaurantResult);exit();

    $this->_fetch("admin_index");
  }

  function request_add(){
    $this->_assign("adminIndex", TRUE);
  	$this->load->model("post_model","postModel");
    $where["where"]["status"] = "0";
    $where["where"]["type"] = "restaurant";
  	$requestadd= $this->postModel->getin($where);
  	$this->_assign('check',$requestadd);
    $this->_fetch("request-add");
  }


  function editstatus($id =""){
    $this->_assign("adminIndex", TRUE);
    $this->load->model("post_model","postModel");
    if(empty($id)){
      show_404();
    }
    $whereReport["where"]["id"] = $id;
    $data = $this->postModel->get($whereReport);
    $this->_assign("status", $data[0]);
    $this->_fetch("changestatus");

 }
 function newstatus(){
  $this->_assign("adminIndex", TRUE);
  $this->load->model("post_model","postModel");
  $post=$this->input->post();
  $newstatus = $this->postModel->add($post);

    if(!empty($newstatus)){
      redirect(base_url("admin/restaurant/request-add"));
    }else{
      show_error("Can't add record.");
    }
  }
 
function admin_upload(){
    $this->_assign("adminIndex", TRUE);
    $this->load->model('category_model','categoryModel');
    $where["where"]["parent_id"] = "1";
    $travelResult = $this->categoryModel->get($where);
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $this->load->library('googlemaps');
    $config['center'] = '7.966598, 98.359929';
    $config['zoom'] = '10';
    $config['map_height'] = '300px';
    $config['map_width'] = '450px';
    $this->googlemaps->initialize($config);
    $marker = array();
    $marker['position'] = '7.966598, 98.359929';
    $marker['draggable'] = true;
    $marker['ondragend'] = 'addValue(event.latLng.lat(), event.latLng.lng());';
    $this->googlemaps->add_marker($marker);
    $data['map'] = $this->googlemaps->create_map();
    $this->_assign("map", $data['map']);
    $this->_assign("traList",$travelResult);
    $this->_fetch("admin_upload");
  }

  function upload() {
    $this->_assign("adminIndex", TRUE);
    $user = $this->ion_auth->user_array();
    $whereRest["name"]  = $this->input->post('name');
    $whereRest["impression"]  = $this->input->post('impression');
    $whereRest["content"] = $this->input->post('content');
    $whereRest["comment_status"] = $this->input->post('comment_status');
    $whereRest["tag_def"] = $this->input->post('tagDefault');
    $whereRest["tag"] = $this->input->post('tag');
    $whereRest["lat"]  = $this->input->post('lat');
    $whereRest["lng"] = $this->input->post('lng');
    $whereRest["update_date"] = $this->input->post('update_date');
    $whereRest["cr_date"] = $this->input->post('cr_date');
    $whereRest["lu_date"] = $this->input->post('lu_date');
    $whereRest["status"] = "1";
    $whereRest["usr_id"] = $user['id'];
    $whereRest["usr_ip"] = $user['ip_address'];
    if(isset($_POST['upload'])) {
      $addResult = $this->restaurantModel->add($whereRest);
       if(empty($addResult)){
        log_message("error","Can't add restaurant.");
       redirect(base_url("admin/restaurant"));

    }else{
      $post_id = mysql_insert_id();
      $this->load->model('images_model','imageModel');
      $this->load->model('category_post_model','category_postModel');
      foreach($whereRest["tag"] as $key => $value){
        $tag['pos_id'] = $post_id;
        $tag['cat_id'] = $value;
        $addResult = $this->category_postModel->add($tag);
      }
      $gallery_path = realpath(APPPATH . '../themes/panhin/images/upload/');
      $config['upload_path'] = $gallery_path;
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';

      $this->load->library('upload', $config);
 
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['new_image'] = '';
      $configThumb['maintain_ratio'] = TRUE;
      $configThumb['width'] = 210;
      $configThumb['height'] = 140;

      $configThumb1 = array();
      $configThumb1['image_library'] = 'gd2';
      $configThumb1['source_image'] = '';
      $configThumb1['create_thumb'] = TRUE;
      $configThumb1['new_image'] = '';
      $configThumb1['maintain_ratio'] = TRUE;
      $configThumb1['width'] = 410;
      $configThumb1['height'] = 340;

     

      $this->load->library('image_lib');

      $whereImage["pos_id"] = $post_id;
      $first=0; 
      for($i = 1; $i < 5; $i++) {

        $upload = $this->upload->do_upload('image'.$i);

        if($upload === FALSE) continue;
     
        $data = $this->upload->data();

        $uploadedFiles[$i] = $data;

        if($data['is_image'] == 1) {
          $configThumb['source_image'] = $data['full_path'];
          $configThumb['new_image'] = $data['file_path']."/image_small";
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
          $configThumb1['source_image'] = $data['full_path'];
          $configThumb1['new_image'] = $data['file_path']."/image_medium";
          $this->image_lib->initialize($configThumb1);
          $this->image_lib->resize();
          $first++;
        if($first == 1) {
          $whereImage["firstimage"] = "1";
        }else{
          $whereImage["firstimage"] = "0";
        }
        $whereImage["title"]  = $this->input->post('taxt'.$i);
        $whereImage["filesize"] = $data['file_size'];
        $whereImage["filename"] = $data['file_name'];
        $whereImage["filepath"] = $data['file_path'];
        $whereImage["small"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["medium"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["large"] = $data['file_name'];
        $whereImage["fullpath"] = $data['full_path'];
        $whereImage["type"] = $data['image_type'];
        $addImg = $this->imageModel->add($whereImage);
           
        }
        
      }
    }
        redirect(base_url("admin/restaurant"));
    }
  }

  function edit($id=""){
    $this->_assign("adminIndex", TRUE);
    $this->_assign("user", $this->ion_auth->user_array());
    $this->load->model('restaurant_model','restaurantModel');
    $where["where"]["id"] = $id;
    $restaurantResult = $this->restaurantModel->getTag($where);
    $this->_assign("resDetail",$restaurantResult[0]);

      if(!empty($restaurantResult[0]["lat"]) and !empty($restaurantResult[0]["lng"])){
        $this->load->library('googlemaps');
        $config['center'] = $restaurantResult[0]["lat"].','. $restaurantResult[0]["lng"];
        $config['zoom'] = '15';
        $config['map_height'] = '300px';
        $config['map_width'] = '450px';
        $this->googlemaps->initialize($config);

        $marker = array();
        $marker['position'] = $restaurantResult[0]["lat"].','. $restaurantResult[0]["lng"];
        $marker['draggable'] = true;
        $marker['ondragend'] = 'addValue(event.latLng.lat(), event.latLng.lng());';
        $marker['title'] = $restaurantResult[0]["title"];
        $this->googlemaps->add_marker($marker);
        $data['map'] = $this->googlemaps->create_map();
        $this->_assign("map", $data['map']);
      }

    $this->_fetch("add");
  }

  function del($id = ""){
    $this->_assign("adminIndex", TRUE);
    if(!$this->ion_auth->logged_in()){
      redirect(base_url("admin"),"refresh");
    }
    if(empty($id)){
      redirect(base_url("admin/restaurant"),"refresh");
    }
    $userdata = $this->ion_auth->user_array();
    $result = $this->restaurantModel->get($id);

    if($result[0]["usr_id"] == $userdata['id'] OR $this->ion_auth->in_group("admin")){
      //$delete["id"] = $id;
      //$delete["status"] = 0;
      $result = $this->restaurantModel->delete($id);
      //$this->cache->delete_group('post_');
    }else{
      show_404();
    }
    redirect(base_url("admin/restaurant"));
  }

  function edit_img($id = ""){
    

    $this->_fetch("edit_img");
  }


  function request_del(){
    $this->_assign("adminIndex", TRUE);
    $this->load->model('restaurant_model','restaurantModel');
    $where["where"]["status"] = "2";
    $where["where"]["type"] = "restaurant";
    $requestadd= $this->restaurantModel->getdel($where);
    $this->_assign('check',$requestadd);
    //var_dump($requestadd);exit();
    $this->_fetch("request-del");
  }

  function delete_request($id = ""){
    $this->_assign("adminIndex", TRUE);
    if(!$this->ion_auth->logged_in()){
      redirect(base_url("admin"),"refresh");
    }
    if(empty($id)){
      redirect(base_url("admin/restaurant"),"refresh");
    }
    $userdata = $this->ion_auth->user_array();
    $result = $this->restaurantModel->get($id);

    if($result[0]["usr_id"] == $userdata['id'] OR $this->ion_auth->in_group("admin")){
      //$delete["id"] = $id;
      //$delete["status"] = 0;
      $result = $this->restaurantModel->delete($id);
      //$this->cache->delete_group('post_');
    }else{
      show_404();
    }
    redirect(base_url("admin/restaurant/request-del"));
  }


  function changedelstatus($id=""){
    $this->_assign("adminIndex", TRUE);
    $this->load->model("post_model","postModel");
    if(!empty($id)){
        $whereReport["status"] = "0";
        $whereReport["where"]["id"] = $id;
        $data = $this->postModel->update($whereReport);
        redirect(base_url("admin/restaurant/request-del"));
    }
    else{
    redirect(base_url("admin/restaurant"));
    }
  }

  function add_img(){
    $this->_assign("adminIndex", TRUE);
    if (!empty($_FILES))
  {

      $tempFile = $_FILES['file']['tmp_name'];

      $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';

      $targetFile = $targetPath ."restaurant_". $_FILES['file']['name'];

      if(move_uploaded_file($tempFile, $targetFile))
      {
        //var_dump(move_uploaded_file($tempFile, $targetFile));exit();
        //$this->_assign("tempFile",$tempFile);
        //$this->view->disable();
        echo $tempFile;
        //echo json_encode($tempFile);
      }


  }
  if(!empty($_POST['v_file_to_remove']))
  {
      //redirect(base_url("admin/restaurant"));
      //echo "fuck!!!";
      $uploads_directory = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
      $attached_files = $uploads_directory.strip_tags($_POST['v_file_to_remove']);
      @chmod($attached_files,0777);
      @unlink($attached_files);
      
      //mysql_query("delete from `vpb_uploads` where `name` = '".mysql_real_escape_string(strip_tags($_POST['v_file_to_remove']))."'");

  }

}

  function show_detail($id=""){
    if(!empty($id)){
    $this->_assign("isDetail", TRUE);
    $this->load->model('restaurant_model','restaurantModel');
    $this->load->model('images_model','imagesModel');
    $where["where"]["id"] = $id;
    $whereImg["where"]["pos_id"] = $id;
    $whereReview["where"]["parent_id"] = $id;
    $restaurantResult = $this->restaurantModel->get($where);
    $restaurantImage = $this->imagesModel->get($whereImg);
    $restaurantReview = $this->restaurantModel->get($whereReview);
    if(!empty($restaurantReview)){
    foreach($restaurantReview as $key => $value){
    $whereImgRev[$value["id"]]["pos_id"] = $value['id'];
    $whereImgRev[$value["id"]]["impression"] = $value['impression'];
    $whereImgRev[$value["id"]]["content"] = $value['content'];
    $whereRevImg["where"]["pos_id"] = $value['id'];
    $restaurantRevImage = $this->imagesModel->get($whereRevImg);
    $whereImgRev[$value["id"]]["image"] = $restaurantRevImage;
    }
  }  
   //var_dump($whereImgRev);exit;
    $this->_assign("revList",$whereImgRev);
    $this->_assign("resList",$restaurantResult);
    $this->_assign("imgList",$restaurantImage); 
    foreach($restaurantResult as $key => $res )
      if(!empty($res["lat"]) and (!empty($res["lng"]))){
        $this->load->library('googlemaps');
        $config['center'] = $res["lat"].','. $res["lng"];
        $config['zoom'] = '15';
        $config['map_height'] = '307px';
        $config['map_width'] = '307px';
        $this->googlemaps->initialize($config);

        $marker = array();
        $marker['position'] = $res["lat"].','. $res["lng"];
        $marker['title'] = $res["impression"];
        $this->googlemaps->add_marker($marker);
        $data['map'] = $this->googlemaps->create_map();
        $this->_assign("map", $data['map']);
      }
    $this->_fetch("detail_restaurant");
    }
    else {
      redirect(base_url());
    }
 }


  function cat(){
    $this->load->model('restaurant_model','restaurantModel');
    PageUtil::addVar("title",$this->lang->line("rest_admin_title"));
    $where["order"] = "pos_cr_date desc";
    $restaurantResult = $this->restaurantModel->getTag($where);
    $this->_assign("resList",$restaurantResult);
    //var_dump($restaurantResult);exit();
    $this->_fetch("cat");
  }
  
  function review($status="",$id="",$parent_id=""){
    $this->_assign("user", $this->ion_auth->user_array());
    $this->load->model('comment_model','commentModel');
    $post = $this->input->post();
    if(!empty($post["submit"])){
      $addComment = $this->commentModel->add($post);
      if(empty($addComment)){
              log_message("error","Can't add restaurant.");
            }
      else {
        $wherePost["where"]["id"] = $addComment;
        //$wherePost["select"] = ["pos_id"];
        $commentResult = $this->commentModel->getUsr($wherePost);
        //var_dump($commentResult);exit();
        redirect(base_url("restaurant/show_detail/".$commentResult[0]["pos_id"]));
      }
    }
    else{
      if($status == "edit" && !empty($id)){
        if(empty($parent_id)){
          $parent_id = "0";
        }
        $this->load->model('comment_model','commentModel');
        $whereCom["where"]["id"] = $id;
        //$whereCom["where"]["parent_id"] = $parent_id;
        $commentResult = $this->commentModel->getUsr($whereCom);
        $this->_assign("comList",$commentResult[0]);
        $this->_fetch("review");
      }
      else if ($status == "reply" && !empty($id)){
        $this->_assign("pos_id", $id);
        $this->_assign("parent_id", $parent_id);
        $this->_fetch("review");
      }
      else{

      $this->_assign("pos_id", $id);
      $this->_fetch("review");
    }
    }
    
  }

  function user_review($id=""){
    if(!empty($id)){
    $this->load->model('restaurant_model','restaurantModel');
    $this->load->model('images_model','imagesModel');
    $where["where"]["id"] = $id;
    $restaurantResult = $this->restaurantModel->get($where);
    $this->_assign("resList",$restaurantResult);
    foreach($restaurantResult as $key => $res )
    $this->_fetch("user_review");
    }
    else {
      redirect(base_url());
    }
 }

 function user_review_upload() {
    $user = $this->ion_auth->user_array();
    $whereRest["name"]  = $this->input->post('name');
    $whereRest["parent_id"]  = $this->input->post('id');
    $whereRest["impression"]  = $this->input->post('impression');
    $whereRest["content"] = $this->input->post('content');
    $whereRest["comment_status"] = $this->input->post('comment_status');
    $whereRest["update_date"] = $this->input->post('update_date');
    $whereRest["cr_date"] = $this->input->post('cr_date');
    $whereRest["lu_date"] = $this->input->post('lu_date');
    $whereRest["status"] = "1";
    $whereRest["usr_id"] = $user['id'];
    $whereRest["usr_ip"] = $user['ip_address'];
    if(isset($_POST['upload'])) {
      $addResult = $this->restaurantModel->add($whereRest);
       if(empty($addResult)){
        log_message("error","Can't add restaurant.");
       redirect(base_url("home"));

    }else{
      $post_id = mysql_insert_id();
      $this->load->model('images_model','imageModel');
      $this->load->model('category_post_model','category_postModel');
      foreach($whereRest["tag"] as $key => $value){
        $tag['pos_id'] = $post_id;
        $tag['cat_id'] = $value;
        $addResult = $this->category_postModel->add($tag);
      }
      $gallery_path = realpath(APPPATH . '../themes/panhin/images/upload/');
      $config['upload_path'] = $gallery_path;
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';

      $this->load->library('upload', $config);
 
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['new_image'] = '';
      $configThumb['maintain_ratio'] = TRUE;
      $configThumb['width'] = 210;
      $configThumb['height'] = 140;

      $configThumb1 = array();
      $configThumb1['image_library'] = 'gd2';
      $configThumb1['source_image'] = '';
      $configThumb1['create_thumb'] = TRUE;
      $configThumb1['new_image'] = '';
      $configThumb1['maintain_ratio'] = TRUE;
      $configThumb1['width'] = 410;
      $configThumb1['height'] = 340;

     

      $this->load->library('image_lib');

      $whereImage["pos_id"] = $post_id;
      $first=0; 
      for($i = 1; $i < 5; $i++) {

        $upload = $this->upload->do_upload('image'.$i);

        if($upload === FALSE) continue;
     
        $data = $this->upload->data();

        $uploadedFiles[$i] = $data;

        if($data['is_image'] == 1) {
          $configThumb['source_image'] = $data['full_path'];
          $configThumb['new_image'] = $data['file_path']."/image_small";
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
          $configThumb1['source_image'] = $data['full_path'];
          $configThumb1['new_image'] = $data['file_path']."/image_medium";
          $this->image_lib->initialize($configThumb1);
          $this->image_lib->resize();
          $first++;
        if($first == 1) {
          $whereImage["firstimage"] = "1";
        }else{
          $whereImage["firstimage"] = "0";
        }
        $whereImage["title"]  = $this->input->post('taxt'.$i);
        $whereImage["filesize"] = $data['file_size'];
        $whereImage["filename"] = $data['file_name'];
        $whereImage["filepath"] = $data['file_path'];
        $whereImage["small"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["medium"] = $data['raw_name']."_thumb".$data['file_ext'];
        $whereImage["large"] = $data['file_name'];
        $whereImage["fullpath"] = $data['full_path'];
        $whereImage["type"] = $data['image_type'];
        $addImg = $this->imageModel->add($whereImage);
           
        }
        
      }
    }
        redirect(base_url("home"));
    }
  }


}
  

